package PMCXMLParser;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;




/**
 * File_Manager_Impl class contains the methods to perform generic operations
 * on files including the read/write/download the files when URL is given.
 * 
 * @author Hammad Afzal
 * @version 1.0
 */
public class File_Manager_Impl
{

    private String_Manipulator_Impl str_man;
    /**
     * Reads the given input file and put the contents into StringBuffer
     * @param inputFile
     * @return
     */
    
     public StringBuffer fileReader(File inputFile)
    {
        StringBuffer contents = new StringBuffer();

        BufferedReader input = null;
        try
        {
            input = new BufferedReader(new FileReader(inputFile));
            String line = null;
            while((line = input.readLine()) != null)
            {
                contents.append(line);
                contents.append(System.getProperty("line.separator"));
            }
        }
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if(input != null)
                {
                    input.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
        return contents;
    }

    public void URLGenerator(File inputFile)
    {
        StringBuffer contents = new StringBuffer();

        BufferedReader input = null;
        try
        {
            input = new BufferedReader(new FileReader(inputFile));
            String line = null;
            while((line = input.readLine()) != null)
            {
                
            String Final=""+
            "http://dbpedia.org/sparql?default-graph-uri=http://dbpedia.org&query="+
            "SELECT+%3Fabstract+FROM+NAMED+%3Chttp%3A%2F%2Fdbpedia.org%3EWHERE+%7B%7B+%3C"+
            "http%3A%2F%2Fdbpedia.org%2Fresource%2F"+line+"%3E+%3Chttp%3A%2F%2Fdbpedia.org"+
            "%2Fontology%2Fabstract%3E+%3Fabstract.FILTER+langMatches%28+lang%28%3Fabstract"+
             "%29%2C+%27en%27%29+%7D%7D&format=text/csv";

            System.out.println("Write To File :"+Final);
                contents.append(Final);
                fileDownloader(Final,"E:\\PMCXML\\"+line+".txt");
                contents.append(System.getProperty("line.separator"));
            }
            
            fileWriter(contents,  "E:\\URL.txt");
        }
        catch (FileNotFoundException ex)
        {
            ex.printStackTrace();
                            System.out.println(ex);

        }
        catch (IOException ex)
        {
            ex.printStackTrace();
            System.out.println(ex);
        }
        finally
        {
            try
            {
                if(input != null)
                {
                    input.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
                System.out.println(ex);
            }
        }
    }
    
     public void URLGenerator(String input_path)
    {
        File readFile = new File(input_path);
        URLGenerator(readFile);
    }

    /**
     * Reads the file from given path and put the contents into StringBuffer
     * @param input_path
     * @return
     */
    public StringBuffer fileReader(String input_path)
    {
                                

        File readFile = new File(input_path);
        return fileReader(readFile);
    }

    /**
     * Downloads the file from given URL and writes it into the output_path
     * @param url
     * @param output_path
     */
    public void fileDownloader(URL url, String output_path)
    {
        try
        {
            InputStream urlStream = url.openStream();
            InputStreamReader iStr = new InputStreamReader(urlStream);
            BufferedReader reader = new BufferedReader(iStr);

            File writeFile = new File(output_path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStream bout = new BufferedOutputStream(fout);
            OutputStreamWriter writer = new OutputStreamWriter(bout,
                    "ISO-8859-1");
            String line = reader.readLine();

            while(line != null)
            {
                
                writer.write(line);
                writer.write(System.getProperty("line.separator"));
                line = reader.readLine();
            }
            urlStream.close();
            reader.close();
            writer.close();
        }
        catch (IOException e)
        {
            System.out.println(e);
        }
    }

    /**
     * Downloads the file from given string url_string and writes it into the output_path
     * @param url_string
     * @param output_path
     */
    public void fileDownloader(String url_string, String output_path)
    {
        try
        {
            URL url = new URL(url_string);
            fileDownloader(url, output_path);
        }
        catch (MalformedURLException ex) {
            System.out.println(ex);
        }
    }

    /**
     * Writes the given String in a file whose path is given as input
     * @param str
     * @param path
     */
    public void fileWriter(String str, String path)
    {
        try
        {
            File writeFile = new File(path);
            OutputStream fout = new FileOutputStream(writeFile);
            OutputStreamWriter writer = new OutputStreamWriter(fout, "ISO-8859-1");
            writer.write(str);
            writer.close();
            fout.close();
        }
        catch (Exception ex)
        {
            System.out.println("Value of Exception is :" + ex.getMessage());
        }
    }

    /**
     * Writes the given StringBuffer in a file whose path is given as input
     * @param stringBuffer
     * @param output_path
     */
    public void fileWriter(StringBuffer stringBuffer, String output_path)
    {
        fileWriter(stringBuffer.toString(),output_path);
    }

    /**
     * Returns the file name without extension
     * @param file_name_extension
     * @param extension
     * @return
     */
    public String strip_file_name_from_file_name_plus_extension(String file_name_extension, String extension)
    {
        int extension_length = extension.length() + 1;
        int inputFileLength = file_name_extension.length();
        file_name_extension = file_name_extension.substring(0, inputFileLength - extension_length);
        return file_name_extension;

    }

    /**
     * Returns the file name without extension
     * @param file_name
     * @param extension
     * @param delimiter
     * @return
     */
    public String strip_file_name_at_extension(String file_name, String delimiter)
    {
        return str_man.strip_string_at_delimiter(file_name,delimiter);

    }

    /**
     * Returns the extension of the input file
     * @param inputFile
     * @return
     */
    public String strip_extension_from_file_name(String inputFile)
    {
        int index_extension = inputFile.indexOf(".");
        int inputFileLength = inputFile.length();
        String extension = inputFile.substring(index_extension + 1, inputFileLength);
        return extension;

    }
}
